package com.jh3502.vivo.function.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.base.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/12/11 10:10
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class ReimTypeChooseDialog extends BaseActivity {
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.submit)
    TextView submit;
    private List<ReimTypeBean> datas = new ArrayList<>(2);
    private String[] names = {"出差报销", "差旅费", "住宿费", "出租车票", "补助", "伙食补助", "交通补助"};
    private boolean[] isRoots = {true, false, false, false, true, false, false,};
    private int[] types = {0, 0, 1, 2, 3, 3, 3,};// 0 差旅，1 住宿 2 出租 3 补助
    private String title;
    private int type;


    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_reim_type_choose;
    }

    @Override
    protected void initView() {


    }

    @Override
    protected void initData() {
        for (int i = 0; i < names.length; i++) {
            ReimTypeBean reimTypeBean = new ReimTypeBean();
            reimTypeBean.setName(names[i]);
            reimTypeBean.setRoot(isRoots[i]);
            reimTypeBean.setType(types[i]);
            datas.add(reimTypeBean);
        }


        ListChoiceAdapter adapter = new ListChoiceAdapter(this, datas);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                title = names[position];
                type = types[position];
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }


    @OnClick(R.id.cancel)
    public void onCancelClicked() {
        onBackPressed();
    }

    @OnClick(R.id.submit)
    public void onSubmitClicked() {
        if (TextUtils.isEmpty(title)) {
            showShortToast("最少选择一项");
            return;
        }
        Intent intent = new Intent();

        intent.putExtra("position", getIntent().getIntExtra("position", 0));
        intent.putExtra("title", title);
        intent.putExtra("type", type);
        setResult(Activity.RESULT_OK, intent);
        onBackPressed();
    }

    private class ListChoiceAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<ReimTypeBean> list;

        public ListChoiceAdapter(Context context, List<ReimTypeBean> list) {
            this.inflater = LayoutInflater.from(context);
            this.list = list;


        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ListChoiceAdapter.ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_reim_type_choice, null);
                holder = new ListChoiceAdapter.ViewHolder();
                holder.root = convertView.findViewById(R.id.root);
                holder.blank = convertView.findViewById(R.id.blank);
                holder.title = convertView.findViewById(R.id.title);
                holder.check = convertView.findViewById(R.id.check);

                convertView.setTag(holder);
            } else {
                holder = (ListChoiceAdapter.ViewHolder) convertView.getTag();
            }
            boolean check = ((ListView) parent).isItemChecked(position);
            holder.check.setChecked(check);
            holder.title.setText(datas.get(position).getName());
            boolean isRoot = datas.get(position).isRoot();
            if (isRoot) {
                holder.root.setBackgroundColor(Color.parseColor("#FFE2E7E9"));
                holder.blank.setVisibility(View.GONE);
                holder.title.setTextColor(Color.parseColor("#FF333333"));
                holder.check.setVisibility(View.GONE);
            } else {
                holder.root.setBackgroundColor(Color.WHITE);
                holder.blank.setVisibility(View.VISIBLE);
                holder.title.setTextColor(Color.parseColor("#FF666666"));
                holder.check.setVisibility(View.VISIBLE);
            }
            return convertView;
        }

        private class ViewHolder {
            TextView title;
            View blank;
            ConstraintLayout root;
            CheckBox check;

        }
    }
}
